define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'product/product/index' + location.search,
                    add_url: 'product/product/add',
                    edit_url: 'product/product/edit',
                    del_url: 'product/product/del',
                    multi_url: 'product/product/multi',
                    import_url: 'product/product/import',
                    table: 'product',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'deploy_type', title: __('Deploy_type'),searchList: {"1":__('cpu'),"2":__('主板'),"3":__('显卡'),"4":__('内存'),"5":__('硬盘'),"6":__('电源'),"7":__('散热'),"8":__('机箱'),"9":__('显示器'),"10":__('配件'),"11":__('外设')}, formatter: Table.api.formatter.normal},
                        {field: 'attr.name', title: __('Attr.name'),operate: false},
                        {field: 'attr_id', title: __('Attr.name'),visible: false, searchList: $.getJSON("product/attr/search_index")},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        {field: 'url', title: __('Url'), operate: 'LIKE', formatter: Table.api.formatter.url},
                        {field: 'jdurl', title: __('Jdurl'), operate: 'LIKE', formatter: Table.api.formatter.url},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'paofen', title: __('Paofen')},
                        {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},

                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'product/product/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'product/product/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'product/product/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {

            $('#c-attr_id').data('params', function (obj) {
                // 获取当前选择的区域ID值
                var areaId = $('#c-deploy_type').val();

                return {
                    // 这里的字段名需要根据你的后端接口要求来定
                    // 通常格式是 custom[字段名]
                    custom: {
                        deploy_type: areaId  // 将区域ID作为筛选参数
                    }
                };
            });

            $('#c-brand_id').data('params', function (obj) {
                // 获取当前选择的区域ID值
                var areaId = $('#c-deploy_type').val();

                return {
                    // 这里的字段名需要根据你的后端接口要求来定
                    // 通常格式是 custom[字段名]
                    custom: {
                        deploy_type: areaId  // 将区域ID作为筛选参数
                    }
                };
            });

            $('#c-deploy_type').on('change', function () {
                var areaId = $('#c-deploy_type').val();

                if (areaId==1||areaId==3||areaId==4||areaId==5){
                    $("#paofen").show();
                } else {
                    $("#paofen").hide();
                }
                if (areaId==1||areaId==2||areaId==3||areaId==4||areaId==5||areaId==7){
                    $("#gonglv").show();
                } else {
                    $("#gonglv").hide();
                }
                // 获取SelectPage实例
                $('#c-attr_id').selectPageClear();
                $('#c-brand_id').selectPageClear();
            });

            Controller.api.bindevent();

        },
        edit: function () {
            $('#c-attr_id').data('params', function (obj) {
                // 获取当前选择的区域ID值
                var areaId = $('#c-deploy_type').val();

                return {
                    // 这里的字段名需要根据你的后端接口要求来定
                    // 通常格式是 custom[字段名]
                    custom: {
                        deploy_type: areaId  // 将区域ID作为筛选参数
                    }
                };
            });

            $('#c-brand_id').data('params', function (obj) {
                // 获取当前选择的区域ID值
                var areaId = $('#c-deploy_type').val();
                /*console.error(areaId)
                if (areaId!=1){
                    $("#brand_id").show();
                } else {
                    $("#brand_id").hide();
                }*/
                return {
                    // 这里的字段名需要根据你的后端接口要求来定
                    // 通常格式是 custom[字段名]
                    custom: {
                        deploy_type: areaId  // 将区域ID作为筛选参数
                    }
                };
            });

            $('#c-deploy_type').on('change', function () {
                if (areaId==1||areaId==3||areaId==4||areaId==5){
                    $("#paofen").show();
                } else {
                    $("#paofen").hide();
                }
                if (areaId==1||areaId==2||areaId==3||areaId==4||areaId==5||areaId==7){
                    $("#gonglv").show();
                } else {
                    $("#gonglv").hide();
                }
                // 获取SelectPage实例
                $('#c-attr_id').selectPageClear();
                $('#c-brand_id').selectPageClear();
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
